# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/12/21
from openpyxl import load_workbook


def get_excel_data(filename, not_empty_column_index=[], ignore_first_line=True, ignore_empty_line=True):
    workbook = load_workbook(filename=filename, read_only=True)
    data_list = []

    if ignore_first_line == True:
        workbook[1:]

    for sheet in workbook:
        for index, item in enumerate(sheet.rows):
            if ignore_empty_line == True:
                tmp_list = []
                is_empty_line = True
                for item2 in item:
                    if item2.value != None:
                        is_empty_line = False
                    tmp_list.append(item2.value)
                if is_empty_line == False:
                    data_list.append(tmp_list)
            else:
                data_list.append([])
                for item2 in item:
                    data_list[index].append(item2.value)

    # 指定列不可为空时，过滤无效数据
    if len(not_empty_column_index) > 0:
        data_list_remove_invalid = []
        for item in data_list:
            is_invalid_line = False
            for i in not_empty_column_index:
                try:
                    if item[i] == "":
                        is_invalid_line = True
                        break
                    else:
                        pass
                except:
                    is_invalid_line = True
                    break
            if is_invalid_line == False:
                data_list_remove_invalid.append(item)
        data_list = data_list_remove_invalid

    return data_list


if __name__ == "__main__":
    import os
    from submodules.common.lib.local_paths import project_path

    file_path = os.path.join(project_path, "test", "data", "测试数据.xlsx")
    # r = get_excel_data(file_path)
    r = get_excel_data(file_path, True)

    print(r)
